# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import re
import gzip,StringIO
import sys,chardet


from HttpClient import HttpClient
class Smzdm(HttpClient):
    def __init__(self):
        self.__pageIndex = 1
        self.__Url = "http://faxian.smzdm.com/9kuai9/p" 

    #正则得到每页商品信息
    def __getAllGoods(self,pageIndex):
        realurl = self.__Url + str(pageIndex)
        pageCode = self.Get(realurl)

        #htmlCharsetGuess = chardet.detect(pageCode)
        #htmlCharsetEncoding = htmlCharsetGuess["encoding"]
        #htmlCode_decode = pageCode.decode(htmlCharsetEncoding)

        type = sys.getfilesystemencoding()
        #print pageCode.decode("UTF-8").encode('mbcs')
        #htmlCode_encode = htmlCode_decode.encode(type)
        #print htmlCode_encode
        pattern = re.compile('<h2 .*?itemName"><a.*?<span .*?black">(.*?)</span><span .*?red">(.*?)</span></a></h2>.*?'+
                             '<div .*?itemUserInfo">.*?<a .*?<span .*?rankTitle">(.*?)</span>.*?<span .*?time">(.*?)</span>',re.S)
        items = re.findall(pattern,pageCode.decode("UTF-8").encode(type))
        for item in items:
            print item[0],item[1],item[2],item[3]

    def start(self):
        print("正在读取前五页白菜价包邮，请稍等...")
        for i in range(1,6):
            print i
            self.__getAllGoods(i)

pc = Smzdm()
pc.start()